<template>
	<view class="user-order10">
		<!-- header -->
		<view class="od-line2" @click="onNav2" v-if="navshow == false">
			<view :class="['item', swiperCurrent1 == item.id ? 'line3-active': ''] " :id="item.id" v-for="(item,index) in navList"
			 :key="index">
				{{item.name}}
			</view>
		</view>

		<swiper :duration="300" @change="changeSwiper" class="ud-swiper" :current="swiperCurrent1">
			<!-- 第一块轮播 -->
			<swiper-item v-for="(row,sub) in 5" :key="sub">
				<!-- 可滚动区域 -->
				<!-- class="scroll-Y" -->
				<scroll-view :scroll-y="true" class="scrollStyle" @scrolltolower="loadMore(sub)">
					<view class="no-orderList" v-if="userOrder.length==0">
						<text>暂无订单</text>
					</view>
					<view v-else class="od-line3" style="padding: 0 15upx;" v-for="(item,index) in userOrder" :key="index">
						<view class="content-line1" @click="navToShop(item)">
							<!-- 54 -->
							<image :src="item.shop_img" mode="aspectFill" style="border-radius: 50%;" class="order-icon"></image>
							<text class="line1-text2 font30">{{item.shop_name}}</text>
							<text class="iconfont enterShop">&#xe600;</text>
							<text class="order-status">{{item.status}}</text>
						</view>
						<view class="content-line2" @click="navDetail(item)">
							<view class="line1-text1">
								<image :src="item.or_avatar" mode="aspectFill"></image>
							</view>
							<view class="line1-text2 font28">
								<view class="content-title">
									{{item.pro_name}}
								</view>
								<view class="price">
									<text class="font30">
										￥{{item.or_price}}
									</text>
									</br>
									<text class="font24">
										×{{item.or_pronum}}
									</text>
								</view>
								<view class="order-number">
									订单号:{{item.order_on}}
								</view>
							</view>

						</view>
						<view class="content-line3">
							<view class="item">
								<text class="item-box">提货日期:</text>
								<text class="item-box1">{{item.get_pro_time}}</text>
							</view>
							<view class="line3-r">
								<text class="font24 ">
									共{{item.or_pronum}}件商品
								</text>
								<text class="font24 padding13">
									合计：
								</text>
								<text class="font30 money">
									￥{{item.totalNum}}
								</text>
							</view>
						</view>
						<view class="content-line4" v-if="sub ==0">
							<view class="operatebtn1" v-if="item.or_status == 0" @click="cancelOrder(item)">
								<text>取消订单</text>
							</view>
							<view class="operatebtn" v-if="item.or_status == 0" @click="toPay(item)">
								<text>去付款</text>
							</view>
							<view class="operatebtn" v-if="item.or_status == 1" @click="refund(item)">
								<text>仅退款</text>
							</view>
							<view class="operatebtn1" v-if="item.or_status == 2" @click="confirmReceipt(item)">
								<text>确认收货</text>
							</view>
							<view class="operatebtn" v-if="item.or_status == 1" @click="pickUpCode(item)">
								<text>提货码</text>
							</view>
							<view class="operatebtn" v-if="item.or_status == 3" @click="navEvaluation(item)">
								<text>评价</text>
							</view>
						</view>
						<view class="content-line4" v-if="sub ==1">
							<view class="operatebtn1" @click="cancelOrder(item)">
								<text>取消订单</text>
							</view>
							<view class="operatebtn" @click="toPay(item)">
								<text>去付款</text>
							</view>
						</view>
						<view class="content-line4" v-if="sub ==2">
							<view class="operatebtn1" @click="confirmReceipt(item)">
								<text>确认收货</text>
							</view>
							<view class="operatebtn" @click="pickUpCode(item)">
								<text>提货码</text>
							</view>
						</view>
						<view class="content-line4" v-if="sub == 3">
							<view class="operatebtn" @click="pickUpCode(item)">
								<text>提货码</text>
							</view>
						</view>
						<view class="content-line4" v-if="sub == 4">
							<view class="operatebtn" @click="navEvaluation(item)">
								<text>评价</text>
							</view>
							<view class="operatebtn1" @click="navToReturns(item)">
								<text>退货退款</text>
							</view>
						</view>
					</view>
					<!-- 上拉加载区域 -->
					<view v-show="isUpLoading" class="load">
						<view class="load-tip">{{loadingText}}</view>
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				navshow:false,
				loadingMask: false,
				isUpNoMore: false, // 上拉加载: 是否显示 "-- END --"
				isUpLoading: false, // 上拉加载: 是否显示 "加载中..."
				loadingText: '', //加载文字
				swiperCurrent1: 0,
				userCurrent_page: 1,
				userOrder: [],
				navList: [{
					id: '0',
					name: '全部'
				}, {
					id: '1',
					name: '待付款'
				}, {
					id: '2',
					name: '配送中'
				}, {
					id: '3',
					name: '待自提'
				}, {
					id: '4',
					name: '已收货'
				}]
			}
		},
		onHide() {
			// uni.removeStorageSync('user-orderId');
			let id = uni.getStorageSync('user-orderId');
			
			if(id){
				this.getOrderList(id)
				this.swiperCurrent1 = id - 1
			}else{
				this.getOrderList(1)
				this.swiperCurrent1 = 0
			}
			
			
		},
		onShow() {
			this.userCurrent_page = 1
			let token = uni.getStorageSync('lph-user-info');
			console.log(token)
			if(token){
				// this.getCartAndCollectNums();
				let id = uni.getStorageSync('user-orderId');
				// uni.removeStorageSync('storage_key');
				console.log(id)
				if (id) {
					this.getOrderList(id)
					this.swiperCurrent1 = id - 1
				}else{
					console.log("123456")
					this.getOrderList(1)
				}
				this.userCurrent_page = 1
				
			}else{
				this.navshow = true
			}
			
			
		},
		onLoad(id) {
			// let token = uni.getStorageSync('lph-user-info');
			// if(token){
			// 	if (id) {
			// 		console.log(id)
			// 		let Id = id.id
			// 		this.getOrderList(Id)
			// 		this.swiperCurrent1 = Id - 1
			// 	} else {
			// 		this.getOrderList(1)
			// 		this.swiperCurrent1 = 0
			// 	}
			// 	let listNode = uni.createSelectorQuery().select(".user-order10")
			// 	this.userCurrent_page = 1
			// }
			
		},
		methods: {
			navToReturns(item) {
				let id = item.orid
				uni.navigateTo({
					url: '/pages/user/refund?id=' + JSON.stringify(id)
				})
			},
			refund(item) {
				console.log(item)
				let id = item.orid
				uni.navigateTo({
					url: '/pages/user/onlyRefund?id=' + JSON.stringify(id)
				})
			},
			navEvaluation(item) {
				let id = item.orid
				uni.navigateTo({
					url: '/pages/order/assess/commentPage?id=' + JSON.stringify(id)
				})
			},
			pickUpCode(item) {
				uni.navigateTo({
					url: '/pages/user/pickUpCode?item=' + JSON.stringify(item)
				})
			},
			confirmReceipt(item) {
				uni.showModal({
					title: '提示',
					content: '是否确认收货？',
					success: res => {
						if (res.confirm) {
							this.commitReceipt(item)
						} else if (res.cancel) {

						}
					}
				});
			},
			//确认收货
			async commitReceipt(item) {
				uni.showLoading({
					title: '加载中'
				});
				let res = await this.$http.receiptShopOrder({
					action: "sure_order",
					order_id: item.orid
				});
				uni.hideLoading();
				if (res.code == 200) {
					uni.showToast({
						title: '收货成功！',
						icon: 'none',
						duration: 2000
					});
					this.swiperCurrent1 = 4
					this.getOrderList(5)
					this.userCurrent_page = 1
				} else {
					uni.showToast({
						title: '操作失败！',
						icon: 'none',
						duration: 2000
					});
				}
			},
			//去付款
			async toPay(item) {
				console.log("===========")
				console.log(item)
				let option = {}
				option.order_id = item.order_id
				option.price = item.totalNum
				uni.navigateTo({
					url:'/pages/discountMall/payAnew?option='+ JSON.stringify(option)
				})
				// let res = await this.$http.unpaidOrderPay({
				// 	action: "unpaid_order",
				// 	order_id: item.order_id
				// });

				// uni.requestPayment({
				// 	provider: 'alipay',
				// 	orderInfo: res.data.orderInfo, //微信、支付宝订单数据 -- 后台返回
				// 	success: (res) => {
				// 		console.log('success:' + JSON.stringify(res));
				// 		clearInterval(this.intervalId);
				// 		this.payFlag = true;
				// 		uni.navigateTo({
				// 			url: '/pages/discountMall/finishPay'
				// 		})
				// 	},
				// 	fail: (err) => {
				// 		console.log('fail:' + JSON.stringify(err));
				// 		clearInterval(this.intervalId);
				// 		uni.showToast({
				// 			title: '中途取消',
				// 			icon: 'none'
				// 		})
				// 	}
				// });
			},

			//取消订单
			cancelOrder(item) {
				uni.showModal({
					title: '提示',
					content: '确定取消订单？',
					success: res => {
						if (res.confirm) {
							this.userCancelOrder(item)
						} else if (res.cancel) {

						}
					}
				});
			},
			async userCancelOrder(item) {
				uni.showLoading({
					title: '加载中'
				});
				let res = await this.$http.receiptShopOrder({
					action: "cancel_order",
					order_id: item.orid
				});
				uni.hideLoading();
				if (res.code == 200) {
					uni.showToast({
						title: '取消成功！',
						icon: 'none',
						duration: 2000
					});
					this.userCurrent_page = 1
					this.getOrderList(1)
					this.swiperCurrent1 = 0
				} else {
					uni.showToast({
						title: '操作失败！',
						icon: 'none',
						duration: 2000
					});
				}
			},
			//加载更多
			loadMore(index) {
				this.isUpLoading = true
				this.loadingText = '加载中...';
				this.userCurrent_page++;
				this.getMore(index + 1);
			},
			// 加载更多
			async getMore(index) {
				let result = await this.$http.userOrder({
					type: index,
					page: this.userCurrent_page
				})
				let data = result.data
				if (result.code == 409 || data.length == 0) {
					this.loadingText = '没有更多数据了';
					return
				}
				data.forEach(item => {
					let mun = Number(item.or_price)
					item.totalNum = parseFloat(this.$format(item.or_pronum * mun)) 
					console.log(item.totalNum)
					item.status = this.$common.orderStatus(item.or_status)
				})
				this.userOrder.push(...data)
				console.log(this.userOrder)
			},
			//我的订单列表
			async getOrderList(type) {
				let res = await this.$http.userOrder({
					type: type,
					page: this.userCurrent_page
				});
				this.userOrder = res.data
				if (this.userOrder.length == 0) {
					this.loadingText = ""
				}
				this.userOrder.forEach(item => {
					let mun = Number(item.or_price)
					item.totalNum = parseFloat(this.$format(item.or_pronum * mun))
					console.log(item.totalNum)
					item.status = this.$common.orderStatus(item.or_status)
				})
			},
			onNav2(e) {
				this.userCurrent_page = 1
				this.swiperCurrent1 = e.target.id
				let type = Number(this.swiperCurrent1) + 1
				this.getOrderList(type)
			},
			changeSwiper(e) {
				this.swiperCurrent1 = e.target.current
			},
			navTo(url) {
				uni.navigateTo({
					url: url
				});
			},
			navToShop(item) {
				console.log(item)
				let id = item.shop_id
				uni.navigateTo({
					url: '/pages/myStore/shop/shop?shid=' + JSON.stringify(id)
				})
			},
			navDetail(item) {
				uni.navigateTo({
					url: '/pages/detail/order-detail?item=' + JSON.stringify(item)
				});
			},
		},
	}
</script>

<style lang="scss" scoped>
	@import 'static/css/user-order.scss';

	.load {
		width: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		margin: 20upx;

		.load-tip {
			font-size: 24upx;
			color: #999999;
			margin-right: 25upx;
		}
	}

	.no-orderList {
		width: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 28upx;
		color: #999999;
		padding-top: 30%;
	}
</style>
